package com.map.util;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * 消息共通类(主要用于EXT请求后给出一个响应消息).
 *
 * @author weidong
 */
@SuppressWarnings({"static-access","unchecked"})
public final class MessageUtil {
    /**
     * 私有构造方法.
     *
     */
    private MessageUtil() {
    }

    /**
     * form提交时向前台抛出异常消息.
     *
     * @param response
     * @param msg 错误消息
     * @author weidong
     */
    public static void failByForm(HttpServletResponse response, String msg){
        response.setCharacterEncoding("UTF-8");
        try{
            response.getWriter().print("{success:false,msg:'"+msg+"'}");
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    /**
     * form提交时向前台抛出成功消息.
     *
     * @param response
     * @param msg
     * @author weidong
     */
    public static void successByForm(HttpServletResponse response, String msg){
        response.setCharacterEncoding("UTF-8");
        try{
            response.getWriter().print("{success:true,msg:'"+msg+"'}");
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    /**
     * ajax抛出异常消息 -- Text格式
     *
     * @param response
     * @param msg
     * @author weidong
     */
    public static void failByAjax(HttpServletResponse response, String msg){
//      response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        try{
            response.getWriter().print(msg);
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    /**
     * ajax抛失败消息  Json格式
     * @param response
     * @param msg
     * @throws IOException
     */
    public static void failJsonByAjax(HttpServletResponse response, String msg) throws IOException{
        response.setContentType("application/json;charset=utf-8");
        PrintWriter print = response.getWriter();
        print.print(msg);
        if (print != null) {
            print.flush();
            print.close();
        }
    }

    /**
     * ajax抛成功消息---Text格式
     * @param response
     * @param msg
     */
    public static void successByAjax(HttpServletResponse response, String msg){
//      response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        try{
            response.getWriter().print(msg);
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    /**
     * ajax抛成功消息---Json格式
     * @param response
     * @param msg
     * @throws IOException
     */
    public static void successJsonByAjax(HttpServletResponse response, String msg) throws IOException{
        response.setContentType("application/json;charset=utf-8");
        PrintWriter print = response.getWriter();
        print.print(msg);
        if (print != null) {
            print.flush();
            print.close();
        }
    }

    /**
     * ajax抛成功消息---xml格式
     * @param response
     * @param msg
     * @throws IOException
     */
    public static void successXmlByAjax(HttpServletResponse response, String msg) throws IOException{
        response.setContentType("application/json;charset=utf-8");
        PrintWriter print = response.getWriter();
        print.print(msg);
        if (print != null) {
            print.flush();
            print.close();
        }
    }

    /**
     * 判断单据是否存在 一个对象是否存在
     * @author 张南
     * @create date Nov 15, 2012
     */
    public static void checkObjExist(Object object) throws Exception{
        if(object == null){
            throw new Exception("单据已被删除!请刷新..");
        }
    }
    /**
     * 判断单据是否存在 ids查出的list是否存在值
     * @author 张南
     * @create date Nov 15, 2012
     */
    public static void checkObjsExist(List objList) throws Exception{
        if(objList.size() == 0){
            throw new Exception("单据已被删除!请刷新..");
        }
    }

    /**
     * 判断单据是否删除,是否审核
     * @author 张南
     * @create date Nov 15, 2012
     */
    public static void checkShenhe(Object object,Integer shenheFlag) throws Exception{
        if(object == null){
            throw new Exception("单据已被删除或不存在!请刷新..");
        } else { //  有单据存在
            if(shenheFlag != null && shenheFlag == 1){ // 已审核
                throw new Exception("单据已审核,不能变动!请刷新..");
            }
        }
    }

    /**
     * 判断单据打印 是否审核
     * @author 张南
     * @create date Nov 15, 2012
     */
    public static void checkPrintShenhe(Integer shenheFlag) throws Exception{
        if(shenheFlag == null || shenheFlag == 0){ // 未审核
            throw new Exception("未审核,不能打印!请刷新..");
        }
    }

    /**
     * 判断是否单价未审核
     * @author 张南
     * @create date Nov 15, 2012
     */
    public static void checkDanjiaShenhe(Integer shenheFlag) throws Exception{
        if(shenheFlag == null || shenheFlag == 0){ // 未审核
            throw new Exception("单价未审核,不能提交反审核申请!请刷新..");
        }
    }
    /**
     * 判断是否单价已审核
     * @author 张南
     * @create date Nov 15, 2012
     */
    public static void checkDanjiaNeedShenhe(Integer shenheFlag) throws Exception{
        if(shenheFlag != null && shenheFlag != 0){ // 未审核
            throw new Exception("单价已审核,无法修改..");
        }
    }
}
